/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2025-04-12
 * Time: 19:59
 */
public class Demo22 {
    public static int count = 0;
    public static void main(String[] args) throws InterruptedException {
        Object locker = new Object();
        //把整个for循环都锁上，i < 500000 和 i++ 和 count 都是串行的总的执行时间更长
        Thread t1 = new Thread(() -> {
            synchronized (locker) {
                for (int i = 0; i < 500000; i++) {
                    count++;
                }
            }
            System.out.println("t1线程结束");
        });
        Thread t2 = new Thread(() -> {
           synchronized (locker) {
               for (int i = 0; i < 500000; i++) {
                   count++;
               }
           }
            System.out.println("t2线程结束");
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(count);
    }
}
